package com.cz.sharding;


import cn.hutool.core.date.DateField;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cz.entity.SCardEntity;
import com.cz.entity.SysLogEntity;
import com.cz.mapper.SCardMapper;
import com.cz.mapper.SysLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shardingdbdate")
public class TestShardingDbDateController {


    @Autowired
    private SysLogMapper sysLogMapper;


    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Autowired
    private SCardMapper sCardMapper;

    @GetMapping("testInsert")
    public void testInsert() {
        for (int i = 0; i <= 10000; i++) {
            SysLogEntity sysLogEntity = new SysLogEntity();
            sysLogEntity.setInfo(RandomUtil.randomString("李刚陈华钱胜耿经耳机", 10));
            sysLogEntity.setCreateTime(RandomUtil.randomDate(new Date(), DateField.MONTH, -1, 10));
            sysLogMapper.insert(sysLogEntity);
        }
    }


    @GetMapping("testQuery")
    public void testQuery() {
        List<SysLogEntity> sysLogEntityList = sysLogMapper.selectList(new LambdaQueryWrapper<SysLogEntity>());
        System.out.println(sysLogEntityList.size());
    }

    @GetMapping("testPageQuery")
    public void testPageQuery() {

        List<SysLogEntity> sysLogEntityList = sysLogMapper.selectList(PageDTO.of(1,20), new LambdaQueryWrapper<SysLogEntity>());
        System.out.println(sysLogEntityList.size());
    }

    @GetMapping("testPageJdbcQuery")
    public void testPageJdbcQuery() {
        List<Map<String, Object>> dataMap1 = jdbcTemplate.queryForList("select * from sys_log limit 1,5");
        System.out.println(JSONUtil.toJsonStr(dataMap1));

        List<Map<String, Object>> dataMap2 = jdbcTemplate.queryForList("select * from sys_log limit 6,11");
        System.out.println(JSONUtil.toJsonStr(dataMap2));
    }


    @GetMapping("del")
    public void del(){
        sysLogMapper.delete(new LambdaQueryWrapper<SysLogEntity>());
    }




    @GetMapping("insertScrad")
    public void insertScrad(){
        for(int i =0;i<=100;i++){
            SCardEntity sCardEntity = new SCardEntity();
            sCardEntity.setCode(RandomUtil.randomString("1234567890", 10));
            sCardEntity.setName(RandomUtil.randomString("李刚陈华钱胜耿经耳机", 10));
            sCardMapper.insert(sCardEntity);
        }
    }
}
